import cv2 as cv
import numpy as np


def main():
    img = cv.imread('../data/numberOne.png')
    imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    _, thresh = cv.threshold(imgGray, 127, 255, cv.THRESH_BINARY)
    contours, _ = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
    cnt = contours[1]

    x, y, w, h = cv.boundingRect(cnt)
    center, size, angle = cv.minAreaRect(cnt)

    imgBackGround = np.zeros_like(img)
    cv.drawContours(imgBackGround, contours, 1, (255, 255, 255), -1)
    cv.rectangle(imgBackGround, (x, y), (x + w, y + h), (0, 255, 0), 2)

    box = cv.boxPoints((center, size, angle)).astype(np.int32)
    cv.drawContours(imgBackGround, [box], 0, (0, 0, 255), 2)

    cv.imshow("img", img)
    cv.imshow("imgBG", imgBackGround)
    cv.waitKey(0)
    cv.destroyAllWindows()


if __name__ == "__main__":
    main()
